package com.fhb.demo.myapp.youmeng.ui.receivers;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import com.fhb.demo.myapp.youmeng.ui.activity.IntentActivity;
import com.fhb.demo.myapp.youmeng.ui.cosum.CostomConstants;
import com.umeng.message.UTrack;
import com.umeng.message.common.UmLog;
import com.umeng.message.entity.UMessage;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2018/6/2.
 */

public class NotificationBroadcast extends BroadcastReceiver {
    private PackageManager mPackageManager;
    private static final String TAG = NotificationBroadcast.class.getName();
    @Override
    public void onReceive(Context context, Intent intent) {
        UmLog.e(TAG, "进入===="+TAG);
        String msg=intent.getStringExtra(CostomConstants.EXTRA_KEY_MSG);
        int type=intent.getIntExtra(CostomConstants.EXTRA_KEY_TYPE,-1);
        try {
            UMessage uMsg=(UMessage) new UMessage(new JSONObject(msg));
            switch (type){
                case CostomConstants.ACTION_DISMISS:
                    UmLog.d(TAG, "dismiss notification");
                    UTrack.getInstance(context).setClearPrevMessage(true);
                    UTrack.getInstance(context).trackMsgDismissed(uMsg);
                    break;
                case CostomConstants.ACTION_CLICK:
                    UmLog.d(TAG, "dismiss notification");
                    UTrack.getInstance(context).setClearPrevMessage(true);
                    UTrack.getInstance(context).trackMsgDismissed(uMsg);

                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        againLoad(context);
    }
    private void againLoad(Context ctx){
        mPackageManager= ctx.getPackageManager();
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
        for(int i=0;i<mAllApps.size();i++){
            ResolveInfo res = mAllApps.get(i);
            String pkg = res.activityInfo.packageName;
            UmLog.e("UmengNotificationService", res.activityInfo.name+"进入====pkg:"+pkg);
            if(pkg.equals("com.fhb.demo.myapp")) {
                //该应用的包名和主Activity
                String cls = res.activityInfo.name;
                UmLog.e("UmengNotificationService", "进入====pkg:"+pkg+"==cls"+cls);
                ComponentName componet = new ComponentName(pkg, cls);
                Intent itent = new Intent();
                itent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                itent.putExtra("tag","push");
                itent.setComponent(componet);
                ctx.startActivity(itent);
                return;
            }
        }

    }
}
